/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.Calendar;

/**
 *
 * @author Welington
 */
public class Debug{
    private boolean show;
    private boolean showClassInfo = false;
    
    public static final int INFO  = 0;
    public static final int ERROR = 1;
    public static final int WARN  = 2;
    public static final int LOG   = 3;
    
    private static String[] labels ;
    
    private static Debug debug = null;

    public static Debug getInstance() {
        if (debug == null)
            debug = new Debug();
        
        return debug;
    }
    
    private Debug() {
        labels = new String[]{"INFO", "ERRO", "WARN" ,"LOG"};
    }
    
    public void show(boolean showClassInfo) {
        show = true;
        this.showClassInfo = showClassInfo;
    }
    
    public void hide() {
        show = false;
    }
    
    public  void print(int mode, String msg) {
        if (show(mode))
            showMessage(null, mode, msg);
    }

    public void print(int mode, Class c, String msg) {
        if (show(mode))
            showMessage(c, mode, msg);
    }

    public void println(int mode, String msg) {
        if (show(mode)) {
            showMessage(null, mode, msg);
            System.out.println("");
        }
    }

    public void println(int mode, Class c, String msg) {
        if (show(mode)) {
            showMessage(c, mode, msg);
            System.out.println("");
        }
    }
    
    private  boolean show(int mode) {
        return mode != LOG || (mode == LOG && show);
    }
    
    private  void showMessage(Class c, int mode, String message) {
        String className = "";
        Calendar calendar = Calendar.getInstance();
        mode = mode % labels.length;
        
        if (c != null) {
            if (c.getSimpleName().length() > 20)
                className = c.getSimpleName().substring(0, 17) + "...";
            else
                className = c.getSimpleName();
        }
        
        String text = "";
        
        if (showClassInfo)
            text = String.format(Constants.getLogIdAndClassFormat(), 
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND),
                calendar.get(Calendar.MILLISECOND),
                labels[mode],
                className,
                message);
        else
            text = String.format(Constants.getLogIdFormat(), 
                calendar.get(Calendar.HOUR_OF_DAY),
                calendar.get(Calendar.MINUTE),
                calendar.get(Calendar.SECOND),
                calendar.get(Calendar.MILLISECOND),
                labels[mode],
                message);
        
        
        
        if (mode == ERROR) {
            System.err.println(text);
            cancel();
        } else if (mode == WARN) {
            System.err.println(text);
        } else {
            System.out.println(text);
        }
    }
    
    private  void cancel() {
        System.out.println("");
        System.exit(0);
    }
}
